package com.central.apps.handler.inspcriterion;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.central.app.common.base.BaseHandler;
import com.central.app.common.constant.AppConstant;
import com.central.app.service.HandlerService;
import com.central.apps.dao.inspection.InspplanDao;
import com.central.apps.dao.inspection.inspregionalDao;
import com.central.common.web.PageResult;
import com.central.common.web.Result;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class InspplanHandler extends BaseHandler {
    @Autowired
    private InspplanDao inspplanDao;
    @Autowired
    private inspregionalDao inspregionalDao;
    @Autowired
    private HandlerService handlerService;
    @Override
    public Object operate(String app, String type, Map<String, Object> params) throws Exception {
        if ("inspplanApp".equals(app)) {
            if ("getInsaenum".equals(type)){

                String inspplannum= MapUtils.getString(params, "inspplannum");
                Page<Map<String,Object>> page = new Page<>(1, 10);
                if (MapUtils.getInteger(params, AppConstant.BIZ_QUERY_PAGE) != null && MapUtils.getInteger(params, AppConstant.BIZ_QUERY_LIMIT) != null) {
                    page = new Page<>(MapUtil.getInt(params, AppConstant.BIZ_QUERY_PAGE), MapUtil.getInt(params, AppConstant.BIZ_QUERY_LIMIT));
                }
                IPage<Map<String, Object>> pageInfo = inspregionalDao.getregionaHandler(page, inspplannum);
                return PageResult.<Map<String, Object>>builder().data(pageInfo.getRecords()).code(0).count(pageInfo.getTotal()).build();
            }
        }
        return super.operate(app, type, params);
    }
    @Override
    public void beforSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
    }

    @Override
    public void afterSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {

        if ("inspplanApp".equals(app)) {
            if ("gq_planarea".equals(objName)) {
                //点检区域选择区域后自动将该区域下的相关资产及点检标准写入到“计划资产”中
                String id = MapUtils.getString(data, "id");
                String insaenum = MapUtils.getString(data, "insaenum");
                //通过区域编号查区域下的  区域子表资产
                List<Map<String,Object>> bysqlList  = inspplanDao.getInspplanHandler(insaenum);
                if (CollectionUtils.isNotEmpty(bysqlList)) {
                    for (int i = 0;i<bysqlList.size();i++){
                        Map<String, Object> assetMap = bysqlList.get(i);
                        String assetnum = MapUtils.getString(assetMap,"assetnum");
                        String assetdesc = MapUtils.getString(assetMap,"assetdesc");
                        String inspnum = MapUtils.getString(assetMap,"inspnum");
                        String inspdesc = MapUtils.getString(assetMap,"inspdesc");
                        Map<String,Object> planassetsMap = new HashMap<>();
                        planassetsMap.put("planareaid",id);
                        planassetsMap.put("assetnum",assetnum);
                        planassetsMap.put("assetdesc",assetdesc);
                        planassetsMap.put("inspnum",inspnum);
                        planassetsMap.put("inspdesc",inspdesc);
                        inspplanDao.insert(planassetsMap);

                    }

                }
                //新增计划资产  并且关联计划区域
            }
        }
    }

}
